module ActiveRecord
  module Acts
    module OAuthProviderInstance
      def self.included(base)
        base.extend ClassMethods
      end

      module ClassMethods
        def acts_as_oauth_provider_instance
          return if self.included_modules.include?(ActiveRecord::Acts::OAuthProviderInstance::ActMethods)
          class_eval do
            include ActiveRecord::Acts::OAuthProviderInstance::ActMethods
            include OAuth::RFC3986Encoder
          end
        end
      end

      module ActMethods
        def request_token_response
          "oauth_token=#{rfc3986_escape(request_token)}&oauth_token_secret=#{rfc3986_escape(request_secret)}"
        end

        def access_token_response
          "oauth_token=#{rfc3986_escape(access_token)}&oauth_token_secret=#{rfc3986_escape(access_secret)}"
        end
      end
    end
  end
end

ActiveRecord::Base.class_eval { include ActiveRecord::Acts::OAuthProviderInstance }
